<?php

namespace App\Http\Controllers\Auth;

use App\Models\Sms;
use App\User;
use App\Http\Controllers\Core\Controller;
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Hash;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/undue';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    /**
     * Handle a registration request for the application.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {



        $this->validator($request->all())->validate();
        $this->validateCode($request);
        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        return $this->registered($request, $user) ?: redirect($this->redirectPath());

    }

    /**
     * 对注册信息进行验证
     *
     * @param  array $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'mobile' => ['required', 'string', 'between:11,11', 'unique:users'],
            'password' => ['required', 'string', 'min:6'],
            'code' => ['required'],
        ], [
            'mobile.required' => '手机号码不能为空',
            'mobile.between' => '手机格式错误',
            'mobile.unique' => '账号已存在',
            'password.required' => '密码不能为空',
            'password.string' => '密码',
            'password.min' => '密码字符长度最少为6',
            'code.required' => '验证码不能为空',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'username' => $data['mobile'],
            'mobile' => $data['mobile'],
            'password' => Hash::make($data['password']),
        ]);
    }
    public function validateCode($inputData)
    {
        $sms = Sms::where(['mobile' => $inputData->get('mobile'), 'code' => $inputData->get('code')])->where('create_time', '>', time() - 600)->get();
        if (!$sms->first()) {
            return back()->withErrors(['code' => '验证码错误'])->withInput($inputData->all());
        }
    }
}
